package com.example.uupaymuch.controller.channel.uupay;

import com.example.uupaymuch.controller.health.UUPayAuthHelper;
import com.example.uupaymuch.controller.notice.uupay.UuTransferComponent;
import com.example.uupaymuch.controller.notice.wudi.TransferComponent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Slf4j
@RestController
public class UupayTransferController {

    @Autowired
    private UuTransferComponent uuTransferComponent;
    @Autowired
    private UUPayAuthHelper uuPayAuthHelper;
    private static final String HMAC_SECRET = "yLTJnhYp6Plr5eFhHi96EVF3j1BS9Nj1ugLbZf6bXb7NxmqPfVqCfE2bMhUv5ZwLzLYW31BRZMpK5AIPZLNPXWaQOe6OnqwgW2UiZV5tdAuapzbR6RkFiYtRDEHkdipN"; // 从安全配置中获取

    private final Random random = new Random();
    private final double successProbability = 0.5; // 50% 的成功概率


    @PostMapping("/api/v1/pay/brazil/transferOrder")
    public ResponseEntity<Map<String, Object>> getTestTransaction(@RequestBody Map<String, Object> request) {

        // 生成一个随机数，并与成功概率进行比较
        boolean isSuccess = random.nextDouble() < successProbability;
        // 模拟入参
        String mchNo = request.get("mchNo") != null ? (String) request.get("mchNo") : "";
        String mchOrderNo = request.get("mchOrderNo") != null ? (String) request.get("mchOrderNo") : "";
        String amount = request.get("amount") != null ? (String) request.get("amount") : "";
        String accountNo = request.get("accountNo") != null ? (String) request.get("accountNo") : "";
        String currency = request.get("currency") != null ? (String) request.get("currency") : "";
        String identityNo = request.get("identityNo") != null ? (String) request.get("identityNo") : "";
        String identityType = request.get("identityType") != null ? (String) request.get("identityType") : "";
        String transferDesc = request.get("transferDesc") != null ? (String) request.get("transferDesc") : "";
        String notifyUrl = request.get("notifyUrl") != null ? (String) request.get("notifyUrl") : "";
        String reqTime = String.valueOf( request.get("reqTime") != null ? request.get("reqTime") : "");
        String sign = request.get("sign") != null ? (String) request.get("sign") : "";

        // 用于有效的pixKey列表（可根据需要处理）
        String evp = request.get("evp") != null ? (String) request.get("evp") : "";
        String phone = request.get("phone") != null ? (String) request.get("phone") : "";
        String email = request.get("email") != null ? (String) request.get("email") : "";
        String cpf = request.get("cpf") != null ? (String) request.get("cpf") : "";

        long randomId = 100000000000000L + (long) (random.nextDouble() * 900000000000000L);
        String transferId = "T" + randomId;

        // 创建主响应 Map
        Map<String, Object> response = new HashMap<>();
        response.put("code", 0);
        response.put("msg", "SUCCESS");

        // 创建 data Map
        Map<String, Object> data = new HashMap<>();
        data.put("transferId", transferId); // 支付中心生成的转账单号
        data.put("mchOrderNo", mchOrderNo); // 商户生成的转账订单号
        data.put("amount", amount); // 转账金额（三位小数）
        data.put("accountNo", accountNo); // 巴西支付pixKey
        data.put("accountName", null); // 收款人姓名
        data.put("bankName", null); // 银行名字
        data.put("state", 1); // 转账状态
        // 将 data 放入主响应 Map
        response.put("data", data);

        // 添加签名
        response.put("sign", sign);

        // 回调
        Map<String, Object> noticeMap = new HashMap<>();
        noticeMap.put("mchNo", mchNo); // 商户号
        noticeMap.put("mchOrderNo", mchOrderNo); // 商户的代付订单号
        noticeMap.put("transferId", transferId); // 系统的代付订单号
        noticeMap.put("amount", amount); // 代付金额（三位小数）
        noticeMap.put("currency", currency); // 货币代码
        noticeMap.put("state", 2); // 转账状态
        noticeMap.put("accountNo", accountNo); // 巴西支付pixKey
        noticeMap.put("transferDesc", transferDesc); // 转账备注信息
        noticeMap.put("createdAt", System.currentTimeMillis()); // 订单创建时间
        noticeMap.put("successTime", System.currentTimeMillis()); // 转账成功时间
        noticeMap.put("reqTime", reqTime); // 请求接口时间
        String noticeSign = uuPayAuthHelper.assemblySign(noticeMap, HMAC_SECRET);
        noticeMap.put("sign", noticeSign);

        // 回调
        uuTransferComponent.doNotifyPay(noticeMap, notifyUrl);

        return ResponseEntity.ok(response);
    }
}